package com.newland.mesdk.demo.device;

import com.newland.mtypex.nseries.NSConnV100ConnParams;
import com.newland.mesdk.demo.activity.BaseActivity;
import com.newland.mesdk.demo.moduleinterface.DeviceControllerInterface;
import com.newland.mesdk.demo.util.Const.MessageTag;

/**
 * Created by YJF on 2015/8/18 0018.
 */
public class K21Device extends AbstractDevice {
	private static final String K21_DRIVER_NAME = "com.newland.me.K21Driver";
	private BaseActivity baseActivity;
	private DeviceControllerInterface controller = null;

	public K21Device(BaseActivity baseActivity) {
		super();
		this.baseActivity = baseActivity;
	}

	@Override
	public void initController() {
		ME3xDeviceDriver me3xDeviceController = new ME3xDeviceDriver(baseActivity);
		controller = me3xDeviceController.initMe3xDeviceController(K21_DRIVER_NAME, new NSConnV100ConnParams());
		baseActivity.showMessage("K21控制器已初始化!", MessageTag.TIP);
		baseActivity.btnStateToWaitingConn();
	}

	@Override
	public void connectDevice() {
		baseActivity.showMessage("K21设备连接中..", MessageTag.TIP);
		try {
			controller.connect();
			baseActivity.showMessage("K21设备成功.", MessageTag.TIP);
		} catch (Exception e1) {
			e1.printStackTrace();
			baseActivity.showMessage("K21链接异常,请检查设备或重新连接...", MessageTag.ERROR);
		}

	}

	@Override
	public void disconnect() {
		new Thread(new Runnable() {
			@Override
			public void run() {
				try {
					if (controller != null) {
						controller.disConnect();
						controller = null;
						baseActivity.showMessage("K21控制器断开成功...", MessageTag.TIP);
						baseActivity.btnStateToWaitingInit();
					}
				} catch (Exception e) {
					baseActivity.showMessage("K21控制器断开异常:" + e, MessageTag.TIP);
				}
			}
		}).start();
	}

	@Override
	public DeviceControllerInterface getController() {
		return controller;
	}

	@Override
	public boolean isControllerAlive() {
		if (controller != null) {
			return true;
		}
		return false;
	}
}
